package com.atguigu.test.factory.agent;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author zp
 * @version 1.0
 * @description: TODO
 * @date 2022/10/17 21:25
 */
public class MapperFactoryBean<T> implements FactoryBean<T> {
    private final Class<T> mapperInterface;

    public MapperFactoryBean(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    @Override
    public T getObject() throws Exception {
        InvocationHandler handler = (proxy, method, args) -> {
            Select select = method.getAnnotation(Select.class);
            String log = select.value().replace("#{uId}", args[0].toString());
            System.out.println("SQL：" + log);
            return args[0] + ",⼩小傅哥,bugstack.cn - 沉淀、分享、成⻓长，让⾃自⼰己和⼈人都能有所收获！ ";
        };
        return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]
                {mapperInterface}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return mapperInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
